package cn.blog.controller.back;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.origin.Employee;
import cn.blog.service.back.EmployeeService;

@WebServlet("/back/backlogin")
public class Backlogin extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		forward("back/backlogin");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取 请求参数
				String username = req.getParameter("username").toString();
				req.getSession().setAttribute("uname", username);
				String password = req.getParameter("password").toString();
				EmployeeService es = new EmployeeService();
				HttpSession session =req.getSession();
				List<Employee> eblst = es.getEmployee(username);
				if (username == null || username == "") {
					req.getSession().setAttribute("checkLabel", "*用户名不能为空！");
					redirect("/back/backlogin");
					
					return;
				} else {
					req.getSession().setAttribute("checkLabel", "");
					if (password == null || password == "") {
						req.getSession().setAttribute("checkLabel", "*密码不能为空！");
						redirect("/back/backlogin");
						return;
					}
				}
				if(eblst.size()>0){
					for (int i = 0; i < eblst.size(); i++) {
						//获取集合中的员工对象
						Employee eb = eblst.get(i);
						if(username.equals(eb.getEmpAccount())&&password.equals(eb.getEmpPassword())){
							//登录成功，将员工保存到session中
							session.setAttribute("eb", eb);
							resp.sendRedirect("backstage");
						}
						else{
							req.getSession().setAttribute("checkLabel", "*密码或用户名错误！");
							redirect("/back/backlogin");
							return;
						}
					}
					
				}else{
					req.getSession().setAttribute("checkLabel", "用户错误或不存在！");
					redirect("/back/backlogin");
				}
				
	}
	
}
